// pages/addressChoose/addressChoose.js




let http = require('../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Tname: '收货地址',
    status: ''
  },

  // onChange(event) {
  //   this.setData({
  //     result: event.detail
  //   });
  //   console.log(event)
  // },
// 地址选择
  chooseOver(e) {
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; //当前页面
    var prevPage = pages[pages.length - 2]; //上一个页面
    prevPage.setData({
      addressid: e.currentTarget.dataset.addressid,
      name: e.currentTarget.dataset.name,
      phone: e.currentTarget.dataset.phone,
      province: e.currentTarget.dataset.province,
      area: e.currentTarget.dataset.area,
      city: e.currentTarget.dataset.city,
      position:e.currentTarget.dataset.position,
      addId:e.currentTarget.dataset.addressid,
      flag:1
    });
    wx.navigateBack({
      delta: 1
    })
  },
// 设置默认
  addressChange(event) {
    let _this = this
    let addressid = event.currentTarget.dataset.addressid.id
    console.log(addressid)
    this.setData({
      checkedAddress: event.detail,
      addressid
    });
    if (event.detail == true) {
      this.setData({
        status: 1
      })
    } else {
      this.setData({
        status: 0
      })
    }
    http.postHttp('/user_address_status', {
      status: _this.data.status,
      id: addressid
    }, {
      token: wx.getStorageSync('token')
    }).then(res => {
      _this.onShow()
      // wx.showToast({
      //   title: '设置成功',
      //   icon: "none",
      //   duration:2000
      // })
    })
  },


// 编辑
  goEditAddress(e) {
    let addressid = e.currentTarget.dataset.addressid.id
    let name=e.currentTarget.dataset.name
    let phone=e.currentTarget.dataset.phone
    let postcode=e.currentTarget.dataset.postcode
    let province=e.currentTarget.dataset.province
    let city=e.currentTarget.dataset.city
    let area=e.currentTarget.dataset.area
    let position=e.currentTarget.dataset.position
    console.log(addressid, 'addressid')
    wx.navigateTo({
      url: `/pages/editAddress/editAddress?addressid=${addressid}&name=${name}&phone=${phone}&postcode=${postcode}&province=${province}&city=${city}&area=${area}&position=${position}`,
    })
  },
  goNewAddress(e) {
    wx.navigateTo({
      url: '/pages/editAddress/editAddress',
    })
  },
  goDelectAddress(e) {
    let addressid = e.currentTarget.dataset.addressid.id
    let taht=this
    wx.showModal({
      title: '删除地址',
      content: '确认删除该地址？',
      showCancel: true, //是否显示取消按钮
      cancelText: "取消", //默认是“取消”
      cancelColor: 'black', //取消文字的颜色
      confirmText: "确认", //默认是“确定”
      confirmColor: '#999999', //确定文字的颜色
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          //点击确定
          // temp.splice(index, 1),
          // that.setData({
          //    tempFilePaths: temp,
          // })
          http.getHttp('/user_address_del', {
            id: addressid
          }, {
            token: wx.getStorageSync('token')
          }).then(res => {
            taht.onShow()
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    http.getHttp('/user_address', {}, {
      token: wx.getStorageSync('token')
    }).then(res => {
      console.log(res, 'addressChoose')
      let tmpArr = res.data.data
      this.setData({
        tmp: tmpArr,
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})